package pedongi.mobile.colorfinder.knowledgebase;

import java.util.ArrayList;

import android.content.SharedPreferences;

public class ColorSet {
	private ArrayList<String> colors;
	private String key;
	public boolean isLoaded;
	
	private ColorSet(String key) {
		this.key = key;
		colors = new ArrayList<String>();
	}
	
	public ColorSet(String key , SharedPreferences pref) {
		this(key);
		String value = pref.getString(key, "UNDEFINED");
		if (!value.equals("UNDEFINED")) {
			String[] colors = value.split(",");
			if (colors.length == 4) {
				for (int i = 0 ; i < colors.length ; i++)
					this.colors.add(colors[i]);
			}
			isLoaded = true;
		}
		else
			isLoaded = false;
	}
	
	public ColorSet(String key , String color1 , String color2 , String color3 , String color4) {
		this(key);
		isLoaded = true;
		colors.add(color1);
		colors.add(color2);
		colors.add(color3);
		colors.add(color4);
	}
	
	public ArrayList<String> getColors() {
		return colors;
	}
	
	public static ColorSet createFromString(String key , String value) {
		String[] colors = value.split(",");
		if (colors.length == 4) {
			ColorSet retval = new ColorSet(key , colors[0] , colors[1] , colors[2] , colors[3]);
			return retval;
		}
		else
			return null;
	}
	
	public void save(SharedPreferences pref) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(key, toString());
		editor.commit();
	}
	
	public void delete(SharedPreferences pref) {
		pref.edit().remove(key).commit();
		//pref.edit().commit();
	}
	
	public String toString() {
		String retval = "";
		if (colors.size() == 4) {
			for (int i = 0 ; i < colors.size() ; i++) {
				retval += colors.get(i);
				if (i != colors.size() - 1)
					retval += ",";
			}
		}
		else
			retval = "set #" + key + ". Namun tampaknya set ini belum didefinisikan. Silahkan update atau tanya ahlinya";
		return retval;
	}
}
